package main

import (
	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go"
	jaegercfg "github.com/uber/jaeger-client-go/config"
	"time"
)

func main() {
	//配置jaeger客户端
	cfg := jaegercfg.Configuration{
		Sampler: &jaegercfg.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		Reporter: &jaegercfg.ReporterConfig{
			LogSpans:           true,
			LocalAgentHostPort: "127.0.0.1:6831",
		},
		ServiceName: "shop_service4",
	}
	//创建一个新的链路
	tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
	if err != nil {
		panic(err)
	}
	//设置全局链路
	opentracing.SetGlobalTracer(tracer)
	defer closer.Close()

	parentSpan := tracer.StartSpan("span")

	//spanA 是parentSpan的子span
	spanA := opentracing.StartSpan("funcAAA", opentracing.ChildOf(parentSpan.Context()))
	time.Sleep(time.Millisecond * 500)
	spanA.Finish()

	time.Sleep(time.Millisecond * 500)

	//spanB 是parentSpan的子span
	spanB := opentracing.StartSpan("funcBBB", opentracing.ChildOf(spanA.Context()))
	// 模拟 spanB 出错
	spanB.SetTag("error", true) // 标记为错误

	spanB.LogKV("event", "error", "message", "something went wrong in funcBBB")
	time.Sleep(time.Millisecond * 1000)
	spanB.Finish()

	//父span结束
	parentSpan.Finish()
}
